package com.localjoycommunity.cpdj.project.system.controller;

import com.localjoycommunity.cpdj.common.interceptor.help.NeedLogin;
import com.localjoycommunity.cpdj.common.utils.poi.ExcelUtil;
import com.localjoycommunity.cpdj.framework.aspectj.lang.annotation.Log;
import com.localjoycommunity.cpdj.framework.aspectj.lang.enums.BusinessType;
import com.localjoycommunity.cpdj.framework.web.controller.BaseController;
import com.localjoycommunity.cpdj.framework.web.domain.AjaxResult;
import com.localjoycommunity.cpdj.framework.web.page.TableDataInfo;
import com.localjoycommunity.cpdj.project.system.domain.SysBeauty;
import com.localjoycommunity.cpdj.project.system.domain.UserBeauty;
import com.localjoycommunity.cpdj.project.system.service.ISysBeautyService;
import com.localjoycommunity.cpdj.project.system.service.IUserBeautyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 风采展示Controller
 *
 * @author leepandar
 * @date 2020-10-30
 */
@RestController
@RequestMapping("/system/beauty")
public class SysBeautyController extends BaseController {
    @Autowired
    private ISysBeautyService sysBeautyService;
    @Autowired
    private IUserBeautyService userBeautyService;

    /**
     * 用户给 新闻点赞
     */
    @NeedLogin
    @PostMapping("/digBeauty")
    public AjaxResult digBeauty(@RequestBody UserBeauty userBeauty, HttpServletRequest req) {
        Integer userId = getUserId(req);
        userBeauty.setUserId(userId);
        int i = userBeautyService.beautyDig(userBeauty);
        return AjaxResult.success(i);
    }

    /**
     * 查询风采展示列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysBeauty sysBeauty) {
        startPage();
        List<SysBeauty> list = sysBeautyService.selectSysBeautyList(sysBeauty);
        return getDataTable(list);
    }

    /**
     * 查询风采展示列表
     */
    @GetMapping("/appList")
    @NeedLogin
    public TableDataInfo appList(SysBeauty sysBeauty, HttpServletRequest req) {
        Integer userId = getUserId(req);
        startPage();
        List<SysBeauty> list = sysBeautyService.selectSysBeautyList(sysBeauty);
        list.forEach(item -> item.setIsDig(userBeautyService.isDig(userId,item.getBeautyId())));
        return getDataTable(list);
    }

    /**
     * 导出风采展示列表
     */
    @Log(title = "风采展示", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysBeauty sysBeauty) {
        List<SysBeauty> list = sysBeautyService.selectSysBeautyList(sysBeauty);
        ExcelUtil<SysBeauty> util = new ExcelUtil<SysBeauty>(SysBeauty.class);
        return util.exportExcel(list, "beauty");
    }

    /**
     * 获取风采展示详细信息
     */
    @GetMapping(value = "/{beautyId}")
    public AjaxResult getInfo(@PathVariable("beautyId") Long beautyId) {
        return AjaxResult.success(sysBeautyService.selectSysBeautyById(beautyId));
    }

    /**
     * 获取风采展示详细信息app
     */
    @GetMapping(value = "/app/{beautyId}")
    public AjaxResult appGetInfo(@PathVariable("beautyId") Long beautyId) {
        return AjaxResult.success(sysBeautyService.selectAppSysBeautyById(beautyId));
    }

    /**
     * 新增风采展示
     */
    @Log(title = "风采展示", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBeauty sysBeauty) {
        return toAjax(sysBeautyService.insertSysBeauty(sysBeauty));
    }

    /**
     * 修改风采展示
     */
    @Log(title = "风采展示", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBeauty sysBeauty) {
        return toAjax(sysBeautyService.updateSysBeauty(sysBeauty));
    }

    /**
     * 删除风采展示
     */
    @Log(title = "风采展示", businessType = BusinessType.DELETE)
    @DeleteMapping("/{beautyIds}")
    public AjaxResult remove(@PathVariable Long[] beautyIds) {
        return toAjax(sysBeautyService.deleteSysBeautyByIds(beautyIds));
    }

    /**
     * 风采展示点赞
     */
    @Log(title = "风采展示", businessType = BusinessType.UPDATE)
    @PutMapping("/like")
    public AjaxResult like(@RequestBody SysBeauty sysBeauty) {
        return toAjax(sysBeautyService.updateSysBeautyClickNum(sysBeauty));
    }
}
